﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Allegro.TaskExecution
{
    /// <summary>
    /// Log class
    /// </summary>
    public static class Log
    {
        /// <summary>
        /// Log at a specified trace level
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventSource"></param>
        /// <param name="eventType"></param>
        /// <param name="traceLevel"></param>
        public static void LogAtTraceLevel(string message, object eventSource, LogEventTypes eventType, TraceLevel traceLevel)
        {
            _Log(message, eventSource, eventType, traceLevel);
        }

        /// <summary>
        /// Log an error
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventSource"></param>
        /// <param name="eventType"></param>
        public static void Info(string message, object eventSource, LogEventTypes eventType)
        {
            _Log(message, eventSource, eventType, TraceLevel.Info);
        }
        /// <summary>
        /// Log a warning
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventSource"></param>
        /// <param name="eventType"></param>
        public static void Warning(string message, object eventSource, LogEventTypes eventType)
        {
            _Log(message, eventSource, eventType, TraceLevel.Warning);
        }
        /// <summary>
        /// Log an error
        /// </summary>
        /// <param name="message"></param>
        /// <param name="eventSource"></param>
        /// <param name="eventType"></param>
        public static void Error(string message, object eventSource, LogEventTypes eventType)
        {
            _Log(message, eventSource, eventType, TraceLevel.Error);
        }
        // Private methods
        private static void _Log(string message, object eventSource, LogEventTypes eventType, TraceLevel level)
        {
            LogEventArgs eventInfo = new LogEventArgs(eventSource, eventType, message, level);
            Fire_LogEvent(eventSource, eventInfo);
            Trace.Write(eventInfo);
            string traceLevel = "";
            switch (level)
            {
                case TraceLevel.Error:
                    traceLevel = "Error";
                    break;
                case TraceLevel.Info:
                    traceLevel = "Info";
                    break;
                case TraceLevel.Off:
                    traceLevel = "Off";
                    break;
                case TraceLevel.Verbose:
                    traceLevel = "Verbose";
                    break;
                case TraceLevel.Warning:
                    traceLevel = "Warning";
                    break;
            }
            Console.WriteLine("{0}: {1}", traceLevel, message);
        }

        private static void Fire_LogEvent(object eventSource, LogEventArgs eventInfo)
        {
            //throw new NotImplementedException();
            // TODO: Need to complete this!!
        }
    }
}
